package com.qin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qin.db.DBAccess;
import com.qin.entity.ExecuteBean;
import com.qin.entity.ExecuteLog;
import com.qin.entity.Function;
import com.qin.enums.RetCode;
import com.qin.mapper.ExecuteMapper;
import com.qin.mapper.FunctionMapper;
import com.qin.service.ExecuteService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExecuteServiceImpl implements ExecuteService {

    @Override
    public JSONObject executeFunction(ExecuteBean executeBean) {

//        System.out.println("service层：executeFunction");

        //执行组件内容
        Function function = new Function();
        function.setFunction_id(executeBean.getFunction_id());
        function.execute(executeBean);

        Map map = new HashMap();
        //拼接返回JSON
        map.put("retCode", RetCode.SUCCESS.getCode());
        map.put("retMsg", RetCode.SUCCESS.getMsg());
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }

    @Override
    public ExecuteBean queryExecuteBean(ExecuteBean executeBean) {

//        System.out.println("service层：queryExecuteBean");

        //调用Dao层
        SqlSession sqlSession = null;
        ExecuteBean executeBean0 = null;

        try {
            sqlSession = DBAccess.getSqlSession();
            ExecuteMapper executeMapper = sqlSession.getMapper(ExecuteMapper.class);

            executeBean0 = executeMapper.queryExecuteBean(executeBean);

        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return executeBean0;
    }

    @Override
    public boolean insertExecuteRecord(ExecuteLog executeLog) {

//        System.out.println("service层：insertExecuteRecord");

        //调用Dao层
        SqlSession sqlSession = null;
        boolean isSuccess = false;

        try {
            sqlSession = DBAccess.getSqlSession();
            ExecuteMapper executeMapper = sqlSession.getMapper(ExecuteMapper.class);

            isSuccess = executeMapper.insertExecuteRecord(executeLog);

        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if(sqlSession != null){
                sqlSession.commit();
                sqlSession.close();
            }
        }
        return isSuccess;
    }

    @Override
    public boolean insertExecuteLog(ExecuteLog executeLog, String log_content) {

//        System.out.println("service层：insertExecuteLog");

        //调用Dao层
        SqlSession sqlSession = null;
        boolean isSuccess = false;

        try {
            sqlSession = DBAccess.getSqlSession();
            ExecuteMapper executeMapper = sqlSession.getMapper(ExecuteMapper.class);

            executeLog.setLog_content(log_content);
            isSuccess = executeMapper.insertExecuteLog(executeLog);

        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if(sqlSession != null){
                sqlSession.commit();
                sqlSession.close();
            }
        }
        return isSuccess;
    }

    @Override
    public JSONObject queryExecuteLog(ExecuteLog executeLog) {

//        System.out.println("service层：queryExecuteLog");

        //调用Dao层
        SqlSession sqlSession = null;
        Map map = new HashMap();
        JSONArray retArray;

        try {
            sqlSession = DBAccess.getSqlSession();
            ExecuteMapper executeMapper = sqlSession.getMapper(ExecuteMapper.class);

            List retList = executeMapper.queryExecuteLog(executeLog);
            retArray = new JSONArray(retList);

        } catch (Exception e) {
            e.printStackTrace();
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", e.getMessage());
            JSONObject retJson = new JSONObject(errMap);

            return retJson;
        }  finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }

        //拼接返回JSON
        map.put("logInfo", retArray);
        map.put("retCode", RetCode.SUCCESS.getCode());
        map.put("retMsg", RetCode.SUCCESS.getMsg());
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }
}
